package edu.lingnan.constroller;

import edu.lingnan.common.Result;
import edu.lingnan.pojo.MyOrder;
import edu.lingnan.pojo.Product;
import edu.lingnan.pojo.ProductOrder;
import edu.lingnan.pojo.User;
import edu.lingnan.service.MyOrderService;
import edu.lingnan.service.ProductOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * @author xBaozi
 * @version 1.0
 * @classname MyOrderController
 * @description 我的订单控制器
 * @date 2022/5/27 18:37
 */
@RestController
@RequestMapping("/my-order")
public class MyOrderController {

    @Autowired
    private MyOrderService myOrderService;

    @Autowired
    private ProductOrderService productOrderService;


    //生成订单
    @RequestMapping("/addOrder")
    public Result addOrder(Long productId,MyOrder order) {
        System.out.println("aaaaaa");
        myOrderService.addOrder(order,productId);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        return result;
    }

    //删除订单
    @RequestMapping("/deleteOrderByOrderId/{orderId}")
    public Result deleteOrderByOrderId(@PathVariable("orderId") Long id ) {

        int row = myOrderService.deleteOrderByOrderId(id);
        Result result = new Result();
        if (row > 0) {
            result.setCode(Result.SUCCESS_CODE);
            result.setMsg("影响行数：" + row );
        } else {
            result.setMsg("影响行数：0");
        }
        return result;
    }


    //查看订单(通过订单编号查询)
//    http://localhost:9090/my-order/selectOrderByOrderId/28477284
    @RequestMapping("/selectOrderByOrderId/{orderId}")
    public Result selectOrderByOrderId(@PathVariable("orderId") Long id ) {

        List<MyOrder> myOrderList = myOrderService.selectOrderByOrderId(id);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(myOrderList);
        result.setCode(200);
        return result;
    }

    //通过商家id查订单记录
//    http://localhost:9090/my-order/selectOrderByStoreId/8746829
    @RequestMapping("/selectOrderByStoreId/{storeId}")
    public Result selectOrderByStoreId(@PathVariable("storeId") Long id ) {

        List<MyOrder> myOrderList = myOrderService.selectOrderByStoreId(id);
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(myOrderList);
        result.setCode(200);
        return result;
    }

    //查看所有个人订单详情MyOrder（通过用户id）
    //http://localhost:9090/my-order/selectAllMyOrder/8247730249
    @RequestMapping("/selectAllMyOrder/{userId}")
    public Result selectAllMyOrder(@PathVariable("userId") Long id ) {

        List<MyOrder> myOrderList = myOrderService.selectAllMyOrder(id);

        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(myOrderList);
        result.setCode(200);
        return result;
    }
//    通过用户id,查看所有商品订单ProductOreder
//    http://localhost:9090/my-order/selectAllMyProductOrder/8247730249
    @RequestMapping("/selectAllMyProductOrder/{userId}")
    public Result selectAllMyProductOrder(@PathVariable("userId") Long id ) {

        List<MyOrder> myOrderList = myOrderService.selectAllMyOrder(id);

        List<Long> orderIdList = new ArrayList<Long>();
        for (MyOrder myOrder : myOrderList) {
            orderIdList.add(myOrder.getId());
        }
        List<ProductOrder> myProductOrderList=new ArrayList<>();
        if (!orderIdList.isEmpty()) {
            myProductOrderList = productOrderService.selectProductOrderByOrderIds(orderIdList);
        }
        Result result = new Result();
        result.setCode(Result.SUCCESS_CODE);
        result.setData(myProductOrderList);
        result.setCode(200);
        return result;
    }

}